package com.yuyou.train.user.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.yuyou.train.service.api.lesson.entity.Lesson;
import com.yuyou.train.service.api.lesson.service.LessonAPI;
import com.yuyou.train.service.api.user.entity.User;
import com.yuyou.train.service.api.user.entity.UserMedalItem;
import com.yuyou.train.service.api.user.entity.UserTotal;
import com.yuyou.train.user.mapper.UserTotalMapper;
import com.yuyou.train.user.service.IUserDataConfigService;
import com.yuyou.train.user.service.IUserLessonProgressService;
import com.yuyou.train.user.service.IUserMedalItemService;
import com.yuyou.train.user.service.IUserMedalRelationService;
import com.yuyou.train.user.service.IUserTotalService;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.constant.user.MedalConfigCons;
import com.yuyou.train.util.enums.user.UserMedalEnums;
import com.yuyou.train.util.tool.Sequence;
import com.yuyou.train.util.tool.UUIDUtil;

@Service
public class UserTotalServiceImpl implements IUserTotalService{

	@Autowired
	private IUserDataConfigService userDataConfigService;
	
	@Autowired
	private UserTotalMapper userTotalMapper;
	
	@Autowired
	private LessonAPI lessonAPI;
	
	@Autowired
	private IUserLessonProgressService userLessonProgressService;
	
	@Autowired
	private IUserMedalItemService userMedalItemService;
	
	@Autowired
	private IUserMedalRelationService userMedalRelationService;
	

	
	@Override
	public int deleteByPrimaryKey(String id) {
		return userTotalMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(UserTotal record) {
		record.setId(Sequence.getSequence().getNextId());
		record.setCreateTime(new Date());
		return userTotalMapper.insert(record);
	}

	@Override
	public UserTotal selectByPrimaryKey(String id) {
		return userTotalMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(UserTotal record) {
		return userTotalMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public UserTotal selectByUserId(String userId) {
		return userTotalMapper.selectByUserId(userId);
	}
	
	public ResBoolSimpleInfo<String> updateVideoTotal(String userId,Long second){	
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		UserTotal userTotal=this.selectByUserId(userId);
		if(userTotal!=null){
			userTotal.setVideoTotal(userTotal.getVideoTotal()==null?0+second:userTotal.getVideoTotal()+second);
			this.updateByPrimaryKeySelective(userTotal);
		}else{
			userTotal=new UserTotal();
			userTotal.setUserId(userId);
			userTotal.setVideoTotal(second);
			this.insert(userTotal);	
		}	
		
		//勋章获得判断
		getMedal(userId,UserMedalEnums.type.MEDAL_01.getValue(), userTotal.getVideoTotal());
		res.setSuccess(true);
		return res;
	}
	
	public ResBoolSimpleInfo<String> updateClassTotal(String userId){	
		UserTotal userTotal=this.selectByUserId(userId);
		if(userTotal!=null){
			userTotal.setClassTotal(userTotal.getClassTotal()==null?0+1:userTotal.getClassTotal()+1);
			this.updateByPrimaryKeySelective(userTotal);
		}else{
			userTotal=new UserTotal();
			userTotal.setUserId(userId);
			userTotal.setClassTotal(Long.valueOf(1));
			this.insert(userTotal);	
		}	
		
		isCodeRight(userId,UserMedalEnums.code.MEDAL_02_02.getValue(),userTotal.getClassTotal());
		isCodeRight(userId,UserMedalEnums.code.MEDAL_02_03.getValue(),userTotal.getClassTotal());
		
		return ResBoolSimpleInfo.getSuccessInfo();
	}
	
	public ResBoolSimpleInfo<String> updateLessonTotal(User user,Lesson lesson) throws Exception{	
		
		UserTotal userTotal=this.selectByUserId(user.getId());
		if(userTotal!=null){
			userTotal.setClassLessonLeaf(userTotal.getClassLessonLeaf()==null?0+1:userTotal.getClassLessonLeaf()+1);
			this.updateByPrimaryKeySelective(userTotal);
		}else{
			userTotal=new UserTotal();
			userTotal.setUserId(user.getId());
			userTotal.setClassLessonLeaf(Long.valueOf(1));
			this.insert(userTotal);	
		}	
		
		//是否完成一章
		boolean finishLesson=false;
		String subjectId=userDataConfigService.selectUserSubjectId(user.getId());	
		String nextLessonId=userLessonProgressService.getUserNextLesson(user.getClassId(), user.getId(), subjectId);
				
		if(StringUtils.isEmpty(nextLessonId)){
			finishLesson=true;
		}else{
			Lesson nextLesson = lessonAPI.selectLessonById(nextLessonId);
			if(!lesson.getParentId().equals(nextLesson.getParentId())){
				finishLesson=true;
			}
		}
		if(finishLesson){
			userTotal.setClassLesson(userTotal.getClassLesson()==null?0+1:userTotal.getClassLesson()+1);
			this.updateByPrimaryKeySelective(userTotal);
			
			isCodeRight(user.getId(),UserMedalEnums.code.MEDAL_02_01.getValue(),userTotal.getClassLesson());
		}
			
		return ResBoolSimpleInfo.getSuccessInfo();
	}
	
	public ResBoolSimpleInfo<String> updateExamFullTotal(String userId,int getScore,int fullScore){	
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		if(getScore<fullScore){
			return ResBoolSimpleInfo.getSuccessInfo();
		}
		UserTotal userTotal=this.selectByUserId(userId);
		if(userTotal!=null){
			userTotal.setExamFullTotal(userTotal.getExamFullTotal()==null?0+1:userTotal.getExamFullTotal()+1);
			this.updateByPrimaryKeySelective(userTotal);
		}else{
			userTotal=new UserTotal();
			userTotal.setUserId(userId);
			userTotal.setExamFullTotal(Long.valueOf(1));
			this.insert(userTotal);	
		}	
		
		//勋章获得判断
		List<String> medal=getMedal(userId,UserMedalEnums.type.MEDAL_03.getValue(), userTotal.getExamFullTotal());
		if(!CollectionUtils.isEmpty(medal)){
			res.setData(medal.get(0));
		}		
		res.setSuccess(true);
		return res;

	}
	
	public ResBoolSimpleInfo<String> updateHw80Total(String userId,int getScore){	
		if(getScore<80){
			return ResBoolSimpleInfo.getSuccessInfo();
		}
		UserTotal userTotal=this.selectByUserId(userId);
		if(userTotal!=null){
			userTotal.setHw80Total(userTotal.getHw80Total()==null?0+1:userTotal.getHw80Total()+1);
			this.updateByPrimaryKeySelective(userTotal);
		}else{
			userTotal=new UserTotal();
			userTotal.setUserId(userId);
			userTotal.setHw80Total(Long.valueOf(1));
			this.insert(userTotal);	
		}	
		
		//勋章获得判断
		getMedal(userId,UserMedalEnums.type.MEDAL_04.getValue(), userTotal.getHw80Total());
		
		return ResBoolSimpleInfo.getSuccessInfo();
	}
	
	public ResBoolSimpleInfo<String> updatecThHwTotal(String userId){	
		UserTotal userTotal=this.selectByUserId(userId);
		if(userTotal!=null){
			
			userTotal.setcThHwTotal(userTotal.getcThHwTotal()==null?0+1:userTotal.getcThHwTotal()+1);
			this.updateByPrimaryKeySelective(userTotal);
		}else{
			userTotal=new UserTotal();
			userTotal.setUserId(userId);
			userTotal.setcThHwTotal(Long.valueOf(1));
			this.insert(userTotal);	
		}
		
		//勋章获得判断
		getMedal(userId,UserMedalEnums.type.MEDAL_05.getValue(), userTotal.getcThHwTotal());
		
		return ResBoolSimpleInfo.getSuccessInfo();
	}
	
	
	
	

	private List<String> getMedal(String userId,String type,long key){
		List<String> medalPic=new ArrayList<>();
		//勋章获得判断
		List<String> codeList=MedalConfigCons.getCode(type, key);
		if(!CollectionUtils.isEmpty(codeList)){		
			for(String code:codeList){
				ResBoolSimpleInfo<UserMedalItem> resItem=userMedalRelationService.addMedal(userId, code);
				if(resItem.isSuccess()){
					UserMedalItem item=resItem.getData();
					medalPic.add(item.getPicUrl());
				}
			}
			 
		}				
		return medalPic;
	}

	private void isCodeRight(String userId,String code,long key){
		//勋章获得判断
		boolean res= MedalConfigCons.isCodeRight(code, key);
		if(res){
			userMedalRelationService.addMedal(userId, code);
		}		
	}
	
	
	

	
	
	
	
	
	
	
		
}